package com.huawei.controller;

import com.alibaba.fastjson.JSON;
import com.huawei.entity.Shangpinsku;
import com.huawei.service.SkuService;

import com.huawei.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SkuController
 * @Description TODO
 * @Author 快乐的大胖
 * @Date 2021/11/30 17:06
 */
@Controller
@RequestMapping("/Sku")
public class SkuController {



    @Resource
    SkuService skuImpl;


    //查询
    @RequestMapping(value = "/chaxun",   produces = "application/json;charset=UTF-8")
    @ResponseBody
    public  String selectAll(
            @RequestParam("limit") Integer limit,
            @RequestParam("page") Integer page
    ){
        Map<String, Object> pars = new HashMap<String, Object>();
        pars.put("offset", (page - 1) * limit);
        pars.put("limit", limit);



        List<Shangpinsku> list = skuImpl.selectAll(pars);
        int count = skuImpl.selectCount(pars);
        Result result = new Result(0, "查询成功", count, list);
        return JSON.toJSONString(result);
    }

    //修改
    @RequestMapping(value = "update", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String update(

            @RequestBody Shangpinsku shangpinsku

       ) {
        int i = skuImpl.update(shangpinsku);
        if (i == 1) {
            Result result=new Result(0, "修改成功", 1, i);
            return  JSON.toJSONString(result);
        } else {
            Result result=new Result(1, "修改失败", 1,null);
            return null;

        }


    }

    //添加
    @RequestMapping(value = "insert", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String insert(
            @RequestBody Shangpinsku shangpinsku
    ) {

        int i = skuImpl.insert(shangpinsku);
        if (i == 1) {
            Result result=new Result(0, "添加成功", 1, i);
            return  JSON.toJSONString(result);
        } else {
            Result result=new Result(1, "添加失败", 1,null);
            return null;

        }


    }

    //删除
    @RequestMapping(value = "detele", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public  String detele(
            @RequestParam ("sku_id")  Integer sku_id
    ){
        int i = skuImpl.delete(sku_id);
        if (i == 1) {
            Result result=new Result(0, "删除成功", 1, i);
            return  JSON.toJSONString(result);
        } else {
            Result result=new Result(1, "删除失败", 1,null);
            return null;

        }

    }



}
